package com.league.zjxn.main.mine

import androidx.lifecycle.LifecycleOwner
import com.blankj.utilcode.util.ActivityUtils
import com.league.zjxn.R
import com.league.zjxn.app.SPSConstance
import com.league.zjxn.entity.Enterprise
import com.league.zjxn.entity.UserInfo
import com.league.zjxn.jetpack.vmodel.UserViewModel
import com.league.zjxn.utils.JsonUtils
import com.module.lemlin.http.Status
import com.module.lemlin.owner.OwnerAbstractActivity
import com.module.lemlin.owner.OwnerViewModeActivity
import kotlinx.android.synthetic.main.activity_mine_firm_info.*
import java.lang.Exception

class MineFirmInfoActivity : OwnerViewModeActivity<UserViewModel>() {

    companion object {
        fun open() {
            ActivityUtils.startActivity(MineFirmInfoActivity::class.java)
        }
    }

    override fun viewModel(): UserViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.activity_mine_firm_info

    override fun stateBar(): StatusBar {
        return StatusBar(view = flFirmInfoBar)
    }

    override fun initView() {
        SPSConstance.userInfo?.let {
            fillData(it)
        }
    }

    override fun initData() {
        viewModel.userInfo()
    }

    override fun initViewMode() {
        viewModel.userInfoLiveData.observe(this as LifecycleOwner, { response ->
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            fillData(data)
        })
    }

    private fun fillData(userInfo: UserInfo) {
        val enterprise: Enterprise = try {
            JsonUtils.objectToClass(userInfo.personal_data, Enterprise::class.java)
        } catch (e: Exception) {
            return
        }
        tvMineFirmInfoTitle.text = enterprise.title
        tvMineFirmInfoContact.text = enterprise.contact
        tvMineFirmInfoMobile.text = enterprise.mobile
        tvMineFirmInfoProvince.text = enterprise.province
        tvMineFirmInfoIndustry.text = enterprise.industry
        tvMineFirmInfoAppeal.text = enterprise.appeal
    }
}